package com.SoftwareEngineering.controller;

import com.SoftwareEngineering.pojo.User;
import com.SoftwareEngineering.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;

@Controller
@RequestMapping("/login")
public class LoginController {
    @Inject
    private UserService userService;

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public void login(@Param("username")String username, @Param("password")String password, Model model){
        User user=userService.findUserByUserName(username);
        if(username==null || "".equals(username)){  //没有输入姓名
            model.addAttribute("status","NoName");
        }else if(user!=null && (password==null||"".equals(password))){ //没有输入密码
            model.addAttribute("status","NoPass");
        }else if(user==null){//数据库没查找到姓名，那么增加到数据库中
            userService.saveUser(new User(username, password));
            model.addAttribute("status","success");
        } else if(!(user.getPasswd().equals(password))) { //姓名正确，输入了密码，但是密码错误
            model.addAttribute("status","failed");
        }else if (user.getPasswd().equals(password)){ //姓名密码均正确
            model.addAttribute("status","success");
        }
    }
}
